package com.mlz.mlzmall.ui.activity.act.main;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.mlz.mlzmall.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * describe :
 * Created by Pm on 2016/5/30.
 * PackageName com.mlz.mlzmall.ui.activity.act.main.
 * ProjectName mlzmall_android.
 */

public class ImagePreviewActivity extends Activity{
    ImageView imageView;
    TextView tv_item;
 //   ImageLoader imageLoader;
//    DisplayImageOptions options;

    String imageUrl;
    String[] urls;
    int item;
    int total;

    List<ImageView> images;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_imagepreview);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        imageUrl = bundle.getString("imageurl");
        urls = bundle.getStringArray("urlimages");
        item = bundle.getInt("item");
        total = urls.length;
        tv_item = (TextView)findViewById(R.id.tv_item);
        tv_item.setText(item+1+"/"+total);
        imageView = (ImageView)findViewById(R.id.imageView);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        images = new ArrayList<>();
        for(int i = 0;i < total;i++){

            ImageView imageView = new ImageView(this);
         //   imageLoader.displayImage(urls[i], imageView, options);
            ImageLoader.getInstance().displayImage(urls[i], imageView);
            images.add(imageView);

        }
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter();
        viewPager.setAdapter(viewPagerAdapter);
        viewPager.setCurrentItem(item);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                tv_item.setText(position+1+"/"+total);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    private class ViewPagerAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return images.size();
        }
        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return arg0 == arg1;
        }
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(images.get(position));
        }
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(images.get(position));

            return images.get(position);
        }
    }
}
